home *** CD-ROM | disk | FTP | other *** search
- Path: news.infoserve.net!usenet
- From: paveltka@unix.infoserve.net (Pavel Tkatchouk)
- Newsgroups: comp.lang.c++
- Subject: Stupid Q: what's wrong?
- Date: Mon, 29 Jan 1996 21:34:11 GMT
- Message-ID: <4ejerd$r84@news.infoserve.net>
- NNTP-Posting-Host: unix.infoserve.net
- X-Newsreader: Forte Free Agent v0.55
-
- Hi, there:
- I'm trying to write a simple program for extracting coordinates from
- text file, shifting them and writing back to a new text file. The
- prototype of the program shown below
- gives different coordinates (which I believe shouldn't be) depending
- of:
-
- WITH line #25 output is:
-
- N0021 X3.274Y-1.25Z.0787R.55F5.
- iCount=0
- iCount=2
- N0021 X4.274Y-1.25Z3.0787R.55F5.
- fD[0]: 1 fD[1]: 0 fD[2]: 3
-
-
-
- WITHOUT line #25 output is:
-
- N0021 X3.274Y-1.25Z.0787R.55F5.
- N0021 X4.274Y1.75Z.0787R.55F5.
- ^^^^
- fD[0]: 1 fD[1]: 0 fD[2]: 3
-
-
- My questions are:
- 1. Why cout<<"\n iCount="<<iCount affects CoordShift()?
- 2. What would you recommend me to change in my prototype to make it
- more efficient?
-
- Thank you,
-
- Pavel
-
- ---begin---
- //Test to extract coordinates from string, shift and write back
- #include <fstream.h>
- #include <stdlib.h>
- #include <string.h>
- void CoordShift(char *, char *, float);
- main ()
- {
- char szFileLine[80], szX_Coord[9]="",szN_Coord[9]="",
- szY_Coord[9]="", szZ_Coord[9]="", *ptr;
- char c, szAxis[]="XYZ";//Coordinate names to be shifted
- int iInd, iCount;
- float fD[]={1.0,0.0,3.0}, fShift;//To shift X+fD[0], etc.
-
- //will be entered
-
- //from keyboard in
- //next version
-
- double dX_Coord,
- dY_Coord,
- dZ_Coord;//Numeric equivalent of
- //strings szX_Coord, etc.
-
- strcpy(szFileLine,"N0021 X3.274Y-1.25Z.0787R.55F5.");//for debug
- //only, szFileLine will be read from text
-
- //file later
- cout<<"\n"<<szFileLine;//Debug, line before coordinate shifting
-
- for (iCount=0;iCount<=2;iCount++)
- {
- if(fD[iCount])
- {
- // cout<<"\n iCount="<<iCount;//line #25, !!!!!!!!!
- CoordShift(szFileLine,szAxis+iCount,fD[iCount]);
- }
- }
- cout<<"\n"<<szFileLine;//Debug, line after coordinate shifting
- cout<<"\n fD[0]: "<<fD[0]<<" fD[1]: "<<fD[1]<<" fD[2]:
- "<<fD[2];//Debug
- return 0;
- }
- void CoordShift(char *szF_L,//Line with coordinates to be extracted
- char *szAx,// axis name to be shifted
- float fSh)//the value of shifting
- {
- char szRest[90];//This part of the line shouldn't be changed
- char *ptr,
- *pC_Beg,//Here starts the coordinate to be shifted
- *blank="", c, szA, szC[10];
- double dC;
- szA=*szAx;
- strcpy(szC,blank);
- ptr = strpbrk(szF_L,&szA);//Where the coordinate starts?
- if(ptr)
- {
- ptr++;
- pC_Beg=ptr;
- c=*ptr;
- while((c>=48&&c<=57)||//while c is digit,-,+ or .
- (c==43)||
- (c==45)||
- (c==46))
- {
- c= *ptr;
- strcat(szC,&c);
- *(szC + strlen(szC)-1)=*"\0";
- ptr++;
- }
- strcpy(szRest,ptr-1);
- *strpbrk(szC,"XYZR")=*"\0";//to eliminate X,Y,Z at the end of
- //the szC
- dC=atof(szC);
- dC +=fSh;
- gcvt(dC,6,szC);
- strcpy(pC_Beg,szC);//writing shifted coordinate back to the
- //original line
- strcpy(pC_Beg + strlen(szC),szRest);//writing unchanged rest
-
- //after the new coordinate
- }
- }
- ---end---
-
-
-